﻿//申购详细讨论函数
var dispagesize = "6";
var dispageindex = "1";


$(function () {

    //enter提交
    $("#comment_content").keypress(function (e) {
        var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
        var chrome = navigator.appName == "Netscape" ? true : false;
        if (ie || chrome) {
            if ((e.keyCode == "10") && (e.ctrlKey))
                addDiscuss();
        }
        else {
            if ((e.keyCode == "13") && (e.ctrlKey))
                addDiscuss();
        }
    });
    $("#valicode").keypress(function (e) {
        var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
        var chrome = navigator.appName == "Netscape" ? true : false;
        if (ie || chrome) {
            if ((e.keyCode == "10") && (e.ctrlKey))
                addDiscuss();
        }
        else {
            if ((e.keyCode == "13") && (e.ctrlKey))
                addDiscuss();
        }
    });
    //添加评论
    $("#btnAddDiscuss").click(function () {
        addDiscuss();
    });
    //加载分页
    if ($("#discusspagelist").length > 0) {
        initSubscribesDiscussPagin($("#divPagination").attr("totalcount"));
    }
})
//添加评论
function addDiscuss() {
    //删除其他弹出层
    var list = art.dialog.list;
    for (var i in list) {
        list[i].close();
    };
    if (!isCookieLogin()) {
        art.dialog({
            width: 300,
            content: '您未登录，请先登录。',
            okVal: '马上登录',
            ok: function () {
                window.location.href = "/View/login.aspx?ReturnUrl=" + window.location.href;
            }
        });
        return;
    }
    else if ($.trim($("#valicode").val()).length != 4) {
        art.dialog({
            width: 300,
            time: 5,
            content: '请填写验证码'
        });
    }
    else {
        var content = $.trim($("#comment_content").val());
        if (content == '') {
            art.dialog({
                width: 300,
                time: 5,
                content: '请认真填写内容'
            });
        } else {
        $.post("/member/ajax/ajax_basic.ashx", { Cmd: 'AddComment', project: $("#btnSubscribe").attr("attrid"), content: content, code: $.trim($("#valicode").val()) }, function (data) {
            if (data == "1") {
                art.dialog({
                    width: 300,
                    time: 3,
                    content: '添加评论成功!',
                    close: function () {
                        location.href = location.href;
                    }
                });
            } else if (data == "-2") {
                art.dialog({
                    width: 300,
                    time: 5,
                    content: '该帐号已经被冻结，请与客服联系!'
                });
            } else if (data == "-3") {
                art.dialog({
                    width: 300,
                    time: 5,
                    content: '认证未通过不允许评论!'
                });
            } else if (data == "-5") {
                art.dialog({
                    width: 300,
                    time: 5,
                    content: '验证码异常，请刷新验证码后提交!'
                });
            } else if (data == "-4") {
                art.dialog({
                    width: 300,
                    time: 5,
                    content: '验证码有误!'
                });
            } else {
                art.dialog({
                    width: 300,
                    time: 5,
                    content: '添加评论失败,请重试!'
                });
            }
        }, "html");
        }
    }
  
}

//加载申购讨论列表
function SubscribeDiscussList() {
    $("#discusspagelist").children().remove();
    $("<li align=\"center\">数据加载中...</li>").prependTo('#discusspagelist');
    jQuery.ajax({
        async: false,
        type: "post",
        url: "/ajaxCross/ajax_invest.ashx",
        dataType: "json",
        data: { Cmd: "GetDisscusslist", projectid: $("#btnSubscribe").attr("attrid"), pagesize: dispagesize, pageindex: dispageindex },
        success: function (jsonstr) {
            $("#discusspagelist").children().remove();
            var html = new Array();
            var str = "";
            if (jsonstr.result == "1") {
                for (var i = 0; i < jsonstr.list.length; i++) {
                         str = "<li>" +
                    "<div class=\"userico\"></div>" +
                    "<div class=\"comment\"><span class=\"name\">" + jsonstr.list[i].userName + "</span>：" + jsonstr.list[i].Content+
                         "<div style=\"color:#999;\">" + jsonstr.list[i].AddDate + "</div>" +
                    "</div>" +
                    "<div style=\"display:" + (jsonstr.list[i].replyContent==""?"none":"block") + ";\">" +
                      "<div style=\"width:15px;height:15px;display:block;float:left;margin-left:12px;*margin-left:8px;margin-right:4px;margin-top:2px;\"><img src=\"http://image.tuandai.com/img/minilogo1.jpg\" style=\"vertical-align: bottom; margin-top: 2px;\" width=\"15\" height=\"15\"></div>" +
                      "<div class=\"commentadmin\">" + jsonstr.list[i].replyContent+
                         "<div style=\"color:#999;\">" + jsonstr.list[i].replyAddDate + "</div>" +
                        "<div name=\"subcomment\"></div>" +
                      "</div>" +
                    "</div>" +
                  "</li>"
                    html.push(str);
                }
                $(html.join("")).prependTo('#discusspagelist');
                if (parseInt(jsonstr.totalcount) != parseInt($("#divPagination").attr("totalcount"))) {
                    initSubscribesDiscussPagin(jsonstr.totalcount);
                }
            }
            else {
                $("<li align=\"center\">暂没有人评论...</li>").prependTo('#discusspagelist');
            }
        }
    });

}


//分页申购讨论列表
function initSubscribesDiscussPagin(recordCount) {
    var num_entries = recordCount;
    $("#divPagination").pagination(num_entries, {
        callback: getSubscribesDiscuss,
        items_per_page: dispagesize,
        num_display_entries: 5,
        current_page: parseInt(dispageindex - 1),
        num_edge_entries: 2,
        link_to: "#divPagination",
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",
        prev_show_always: true,
        next_show_always: true,
        renderer: "defaultRenderer",
        show_if_single_page: false,
        load_first_page: true
    });
}


//获取投资人列表
function getSubscribesDiscuss(page_index, jq) {
    if (parseInt(dispageindex - 1) == page_index) {
        return false;
    }
    dispageindex = (page_index + 1);
    SubscribeDiscussList();
    return false;
}



